<template>
  <MainLayout>
    <div class="category-detail">
      <div v-if="category" class="category-info">
        <h1 class="category-title">{{ category.name }}</h1>
        <p class="category-description">{{ category.description }}</p>
        <div class="category-meta">
          <span>{{ category.document_count }} 篇文档</span>
        </div>
      </div>

      <div class="document-list" v-if="documents.length">
        <div v-for="doc in documents" :key="doc.id" class="document-card">
          <router-link :to="`/documents/${doc.slug}`" class="document-link">
            <h2 class="document-title">{{ doc.title }}</h2>
            <p class="document-description">{{ doc.description }}</p>
            <div class="document-meta">
              <span>{{ doc.created_at }}</span>
              <span>{{ doc.views }} 次浏览</span>
            </div>
          </router-link>
        </div>
      </div>
      <div v-else class="empty-state">该分类下暂无文档</div>
    </div>
  </MainLayout>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import MainLayout from '../components/layout/MainLayout.vue'
import { getCategoryDetail } from '../api/category'
import { getDocumentList } from '../api/document'

const route = useRoute()
const category = ref(null)
const documents = ref([])

// 获取分类详情
const fetchCategoryDetail = async () => {
  try {
    const res = await getCategoryDetail(route.params.slug)
    category.value = res.data
  } catch (error) {
    console.error('获取分类详情失败:', error)
  }
}

// 获取分类下的文档列表
const fetchDocuments = async () => {
  try {
    const res = await getDocumentList({ category: route.params.slug })
    documents.value = res.data
  } catch (error) {
    console.error('获取文档列表失败:', error)
  }
}

onMounted(() => {
  fetchCategoryDetail()
  fetchDocuments()
})
</script>

<style scoped>
.category-detail {
  padding: 20px 0;
}

.category-info {
  margin-bottom: 40px;
}

.category-title {
  font-size: 32px;
  font-weight: bold;
  margin: 0 0 20px;
  color: #333;
}

.category-description {
  font-size: 16px;
  color: #666;
  margin: 0 0 20px;
  line-height: 1.6;
}

.category-meta {
  font-size: 14px;
  color: #999;
}

.document-list {
  display: grid;
  gap: 20px;
}

.document-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s;
}

.document-card:hover {
  transform: translateY(-4px);
}

.document-link {
  display: block;
  padding: 20px;
  text-decoration: none;
  color: inherit;
}

.document-title {
  font-size: 20px;
  font-weight: bold;
  margin: 0 0 10px;
  color: #333;
}

.document-description {
  font-size: 14px;
  color: #666;
  margin: 0 0 10px;
  line-height: 1.5;
}

.document-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #999;
}

.empty-state {
  text-align: center;
  padding: 40px;
  color: #999;
  font-size: 14px;
  background-color: #f5f5f5;
  border-radius: 8px;
}
</style> 